//index.js
const utils = require("../../utils/utils.js");
const biz = require("../../utils/bizUtils.js");
const listUtil = require("../../utils/listUtil.js");
const session = require("../../utils/session.js");
const toast = require("../../utils/toasts.js");
const wxm = require("../../utils/wechat_modules.js");
const com = require("../../utils/commonRequest.js");

import {
  req
} from "../../utils/api.js";

//获取应用实例
const app = getApp()
const globalData = app.globalData;

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    list: [],
    hidden: true,
    scrollTop: 0,
    scrollHeight: 0,
    title: '',
    keyword: null,
    catId: null,
    orderRule: 'HOT',
    waiting: true,
    share_modal: false,
    noMore: false,
    banners: ['https://img.lingxing.site/jx/banner400_5_1.png'],
    ads: ['https://img.lingxing.site/jx/small_banner1_1.png'],
    isIphoneX: globalData.isIphoneX,
    showAllStaffs: false
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  getOrder() {
    var that = this;
    req('/order/jxRecent', {}).then(res => {
      // console.log(res);
      if (res.success) {
        var order = res.result;
        if (order) {
          order.statusStr = biz.orderStatusStr(order.status);
          that.setData({
            order
          });
        }
      } else {}
    });
  },
  onLoad: function(options) {
    var that = this;
  },
  refreshMe: function(usr) {
    this.setData({
      user: usr
    });
    this.getOrder();
  },
  onShow: function() {
    this.refresh();
  },
  pushFormSubmit: function(e) {
    console.log(e);
    console.log("formId:" + e.detail.formId);
    com.saveFormId(e);
  },
  gotoMyOrder: function() {
    wx.switchTab({
      url: '/pages/orderList/orderList',
    })
  },
  gotoStaff: function() {
    var order = this.data.order;
    if (!order || !order.servantId) {
      return;
    }
    wx.navigateTo({
      url: '/pages/staffDetail/staffDetail?id=' + order.servantId,
    })
  },
  gotoOrder: function() {
    var order = this.data.order;
    if (!order) {
      return;
    }
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?id=' + order.id,
    })
  },
  onGotUserInfo: function (e) {
    var that = this;
    toast.showLoading();
    com.saveUserInfo(e, this, function () {
      that.refresh();
    });
  },
  refresh:function() {
    session.login(this.refreshMe);
  },
  getPhoneNumber(e) {
    var that = this;
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData);
    toast.showLoading();
    com.bindPhone(e, this, function () {
      that.refresh();
    });
  },
  gotoMyCut:function() {
    wx.navigateTo({
      url: '/pages/cutList/cutList',
    })
  }
})